package com.xunyin.modules.version2.mtstaff.rest;


import com.xunyin.api.ApiResult;
import com.xunyin.enums.StatusEnum;
import com.xunyin.modules.log.AppLog;
import com.xunyin.modules.mtstore.domain.MtStaff;
import com.xunyin.modules.mtstore.service.StaffService;
import com.xunyin.utils.CommonUtilV;
import com.xunyin.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 共享空间:小程序端店铺员工管理控制器
 * </p>
 * Created by xunyin
 * CopyRight 讯音云商 https://www.tonetn.com
 *
 * @since 2023-4-24
 */
@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(value = "共享空间:小程序端店铺员工管理模块", tags = "共享空间:小程序端店铺员工管理模块")
public class ClientMtStaffController {

    /**
     * 员工接口
     */
    private final StaffService staffService;


    /**
     * 获取小程序端店铺员工列表
     */
    @AppLog(value = "获取小程序端店铺员工列表", type = 1)
    //@AuthCheck
    @PostMapping("/staff/list")
    @ApiOperation(value = "获取小程序端店铺员工列表", notes = "获取小程序端店铺员工列表")
    public ApiResult<List<MtStaff>> getStaffList(HttpServletRequest request, @RequestBody MtStaff mtStaff) {
        List<MtStaff> dataList = staffService.queryStaffList(mtStaff);
        log.info("getUserBalanceList -> dataList={}", dataList.toString());
        return ApiResult.ok(dataList);
    }


    /**
     * 保存员工信息接口
     *
     * @return ApiResult<String>
     */
    @AppLog(value = "保存员工信息接口", type = 1)
    //@AuthCheck
    @PostMapping("/staff/save")
    @ApiOperation(value = "保存员工信息接口", notes = "保存员工信息接口")
    public ApiResult<String> saveHandler(HttpServletRequest request, @RequestBody Map<String, Object> params) {

        String id = params.get("id") == null ? "0" : params.get("id").toString();
        String storeId = params.get("storeId") == null ? "0" : params.get("storeId").toString();
        String category = params.get("category") == null ? "0" : params.get("category").toString();
        String mobile = params.get("mobile") == null ? "" : CommonUtilV.replaceXSS(params.get("mobile").toString());
        String realName = params.get("realName") == null ? "" : CommonUtilV.replaceXSS(params.get("realName").toString());
        String description = params.get("description") == null ? "" : CommonUtilV.replaceXSS(params.get("description").toString());
        String status = params.get("auditedStatus") == null ? StatusEnum.FORBIDDEN.getKey() : CommonUtilV.replaceXSS(params.get("auditedStatus").toString());

        MtStaff mtStaff = new MtStaff();
        if (StringUtil.isNotEmpty(id)) {
            mtStaff = staffService.queryStaffById(Integer.parseInt(id));
        }

        if (mtStaff == null && StringUtil.isNotEmpty(id)) {

            return ApiResult.ok("").setMsg("员工信息不存在");
            //throw new XunyinException("员工信息不存在");
        }

        mtStaff.setStoreId(Integer.parseInt(storeId));
        mtStaff.setRealName(realName);
        mtStaff.setMobile(mobile);
        mtStaff.setAuditedStatus(status);
        mtStaff.setDescription(description);
        mtStaff.setCategory(Integer.parseInt(category));

        if (StringUtil.isEmpty(mtStaff.getMobile())) {

            return ApiResult.ok("").setMsg("手机号码不能为空");
            //throw new XunyinException("手机号码不能为空");
        } else {
            MtStaff tempUser = staffService.queryStaffByMobile(mtStaff.getMobile());
            if (tempUser != null && !tempUser.getId().equals(mtStaff.getId())) {

                return ApiResult.ok("").setMsg("该手机号码已经存在");
                //throw new XunyinException("该手机号码已经存在");
            }
        }

        staffService.saveStaff(mtStaff);
        return ApiResult.ok("").setMsg("保存成功");
        //return new ResponseEntity(HttpStatus.OK);
    }

}
